package com.mall.manager.part.controller;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.collections.MapUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.util.WebUtils;

import com.mall.manager.part.service.ManagePartService;
import com.mall.manager.user.controller.ManageUserController;
import com.mall.untils.Page;


/**
 * 
 * @author LISIQIANG
 * 处理角色增、删、改、查控制层
 */
@Controller
public class ManagePartControlle {

	Logger logger = LoggerFactory.getLogger(ManageUserController.class);
	@Resource ManagePartService partService;
	
	/**
     * 添加角色的转向链接
     * @param request
     * @param response
     * @param modelMap
     * @return
     */
    @RequestMapping(value = "/manager_part_insert")
    public String manager_user_insert(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
    	return "part/manager_part_add";
    }
	
	
	/**
	 * 添加某个角色
	 * @param request
	 * @param response
	 * @param modelMap
	 * @return
	 */
    @RequestMapping(value = "/manager_part_add")   
    //@ResponseBody
    public String manager_part_add(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {   
    	Map<String, Object> paraMap = WebUtils.getParametersStartingWith(request, "");
    	int update=partService.manager_part_add(paraMap);
    	if(update>0){
    		modelMap.addAttribute("info", "修改成功！");
    		return "redirect:/manager_part_list ";
    	}
    	modelMap.addAttribute("info", "修改失败！");
		return "redirect:/manager_part_list ";
    }
    
    /**
     * 修改某个角色信息
     * @param request
     * @param response
     * @param modelMap
     * @return
     */
    
    
    @RequestMapping(value = "/manager_part_update")    
    public String manager_part_update(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {   
    	    	
    	Map<String, Object> paraMap = WebUtils.getParametersStartingWith(request, "");
    	Map<String, Object> part = partService.manager_part_updateone(paraMap);	
    	modelMap.addAttribute("part", part);
    	return "part/manager_part_edit";
    	
    }
    
    /**
     * 修改某个角色
     * @param request
     * @param response
     * @param modelMap
     * @return
     */
    
    
    @RequestMapping(value = "/manager_part_edit")    
    public String manager_part_edit(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {   
    	Map<String, Object> paraMap = WebUtils.getParametersStartingWith(request, "");
    	int update=partService.manager_part_edit(paraMap);
    	
    	if(update>0){
    		modelMap.addAttribute("info", "修改成功！");
    		return "redirect:/manager_part_list?type=1";
    	}
    	modelMap.addAttribute("info", "修改失败！");
		return "redirect:/manager_part_list?type=1 ";
        
    }
    
    /**
     * 删除某个角色
     * @param request
     * @param response
     * @param modelMap
     * @return
     */
    
    @RequestMapping(value = "/manager_part_delete")    
    public String manager_part_delete(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {   
    	Map<String, Object> paraMap = WebUtils.getParametersStartingWith(request, "");
    	int update=partService.manager_part_delete(paraMap);
    	    	
    	if(update>0){
    		modelMap.addAttribute("info", "删除成功！");
    		return "redirect:/manager_part_list?type=2";
    	}
    	
    	modelMap.addAttribute("info", "删除失败！");
		return "redirect:/manager_part_list ";
           
    }
    
    /**
     * 分页查询角色信息
     * @param request
     * @param response
     * @param modelMap
     * @return
     */
   
    @SuppressWarnings("unchecked")
	@RequestMapping(value = "/manager_part_list")  
    
    public String manager_part_list(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {   
    	Map<String, Object> paraMap = WebUtils.getParametersStartingWith(request, "");
    	int type = MapUtils.getIntValue(paraMap, "type");
    	Page page= partService.manager_part_list(request);
    	    	
     	if(null!=page){
	     	modelMap.addAttribute("manager_part_list", (List<Map<String,Object>>)page.getResult());
	     	int totalpagecount = page.getTotalPageCount();
	     	modelMap.addAttribute("totalpagecount", totalpagecount);
	     	int currentpageno = page.getCurrentPageNo();
	     	modelMap.addAttribute("currentpageno", currentpageno);
	     	int previouspage = page.getCurrentPageNo() <= 1 ? 1 : page
					.getCurrentPageNo() - 1;
	     	modelMap.addAttribute("previouspage", previouspage);
	     	int nextpage = page.getCurrentPageNo() >= page.getTotalPageCount() ? page
					.getTotalPageCount() : page.getCurrentPageNo() + 1;
			modelMap.addAttribute("nextpage", nextpage); 
			List<Integer> showNumList = page.getShowNumList();
			modelMap.addAttribute("pagenumlist", showNumList);
			modelMap.addAttribute("type", type);
     	}
         	     	    	
        return "part/manager_part_list";
    }
    
	
}
